<?php


namespace app\index\controller;


use app\common\Base;
use app\common\Captcha;
use app\common\SysMessage;
use app\index\model\User;
use app\index\validate\UserValidator;
use think\Exception;
use think\Request;

class UserLogin extends Base
{
    /**
     * @register 注册
     * @param Request $request
     * @param Userlogin $user
     * @param UserValidator $userValidator
     * @return \think\response\Json
     * @throws Exception
     */
    public function register(Request $request,User $user,UserValidator $userValidator,Captcha $captcha){
        try {
            if (!$userValidator->check($request->post())){
                return responseJson(Base::FAIL,$userValidator->getError());
            }
            $username = $request->post('username');
            $password = md5($request->post('password'));
            $phone = $request->post('phone');
            $email = $request->post('email');
            $verify = $request->post('verify');
            if (!empty($data = $user->findUsername($username))){
                return responseJson(Base::FAIL,'用户名已存在,请重新输入');
            }
            if(!empty($data = $user->findEmail($email))){
                return responseJson(Base::FAIL,'邮箱已存在,请重新输入');
            }
            if (!empty($data = $user->findRegisterPhone($phone))){
                return responseJson(Base::FAIL,'手机号码已存在,请重新输入');
            }
            if (!$captcha->check($verify)) {
                return responseJson(Base::FAIL,'请输入正确的验证码或刷新验证码');
            }
            $salt = md5(time().$username);
            if (!$user->register($username,$password,$phone,$email,$salt)){
                return responseJson(Base::FAIL,'注册失败');
            }
            return responseJson(Base::JUMP_PAGE,'注册成功');
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @findpass 找回密码
     * @param Request $request
     * @param UserValidator $userValidator
     * @param Userlogin $user
     * @return \think\response\Json
     * @throws Exception
     */
    public function findpass(Request $request,UserValidator $userValidator,User $user,Captcha $captcha){
        try {
            if (!$userValidator->check($request->post())){
                return responseJson(Base::FAIL,$userValidator->getError());
            }
            $phone = $request->post('phone');
            $email = $request->post('email');
            $password = md5($request->post('password'));
            $verify = $request->post('verify');
            if (empty($user->findPassPhoneEmail($email,$phone))){
                return responseJson(Base::FAIL,'该用户不存在，请先注册');
            }

            if (!empty($user->findSamePass($phone,$password))){
                return responseJson(Base::FAIL,'请输入正确的密码');
            }

            if (!$captcha->check($verify)) {
                return responseJson(Base::FAIL,'请输入正确的验证码或刷新验证码');
            }

            if(!$user->retrievePassword($phone,$password)){
                return responseJson(Base::FAIL,'修改失败,请重试');
            }
            return responseJson(Base::JUMP_PAGE,'修改成功，跳转页面');
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @login    邮箱登录
     * @param Request $request
     * @param UserValidator $userValidator
     * @param Userlogin $user
     * @return \think\response\Json
     * @throws Exception
     */
    public function login(Request $request,UserValidator $userValidator,User $user,Captcha $captcha){
        try {
            if (!$userValidator->check($request->post())){
                return responseJson(Base::FAIL,$userValidator->getError());
            }
            $email = $request->post('email');
            $password = md5($request->post('password'));
            $verify = $request->post('verify');
          
            if (empty($data = $user->login($email,$password))){
                return responseJson(Base::FAIL,'没有此账号');
            }

            if (!$captcha->check($verify)) {
                return responseJson(Base::FAIL,'请输入正确的验证码或刷新验证码');
            }
            $token = $this->loginData($data['id'],$data['phone'],$data);

            if (!$user->loginlastTime($email,$data['phone'])){
                return responseJson(Base::FAIL,'修改失败');
            }
            return  responseJson(Base::JUMP_PAGE,'登录成功', ['token'=>$token]);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @smsLogin    短信登录
     * @param Request $request
     * @param UserValidator $userValidator
     * @param Userlogin $user
     * @return \think\response\Json
     * @throws Exception
     */
    public function smsLogin(Request $request,UserValidator $userValidator,User $user){
        try {
            if (!$userValidator->check($request->post())){
                return responseJson(Base::FAIL,$userValidator->getError());
            }

            $phone = $request->post('phone');
            $verifyCode = $request->post('verifyCode');
            $phoneKey = 'sms'.md5($phone);
            $code = app('mycache')->get($phoneKey);
            if($code != $verifyCode){
                return responseJson(Base::FAIL,'登录失败，请重试');
            }

            if (empty($data = $user->smsLogin($phone))){
                $username = '商家'.rand(0,99999);
                $salt = md5(time().$username);
                if (!$user->insertPhone($username,$phone,$salt)) {
                    return responseJson(Base::FAIL,'登录失败');
                }
                $smsData = $user->findSmsPhone($username,$phone);
                $token = $this->loginData($smsData['id'],$smsData['phone'],$smsData);
            }else{
                $token = $this->loginData($data['id'],$phone,$data);
                if (!$user->loginlastTime($data['email'],$phone)){
                    return responseJson(Base::FAIL,'修改失败',$data);
                }
            }

            return  responseJson(Base::JUMP_PAGE,'登录成功', ['token'=>$token]);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @sendSms 发送短信
     * @param Request $request
     * @param UserValidator $userValidator
     * @param SysMessage $sysMessage
     * @return \think\response\Json
     * @throws Exception
     */
    public function sendSms(Request $request,UserValidator $userValidator,SysMessage $sysMessage,User $user){
        try {
            if (!$userValidator->check($request->post('phone'))){
                return responseJson(Base::FAIL,$userValidator->getError());
            }
            $phone = (float)$request->post('phone');
            $code = rand(100000,999999);
            $phoneKey = 'sms'.md5($phone);
            app('mycache')->set($phoneKey,$code,180);
            if (!is_null($data = $sysMessage->useSys($phone,$code))) {
                return responseJson(Base::FAIL,"短信发送失败",$data);
            }
            return responseJson(Base::OK,"短信发送成功");
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @verify  验证码图片
     * @return \think\Response
     */
    public function verify(){

        $captcha = new  Captcha();
        return $captcha->entry();
    }

    /**
     * @loginData   登录信息
     * @param $id
     * @param $phone
     * @param $data
     * @return string
     */
    public function loginData($id,$phone,$data){
        $token = $this->makeToken($id,$phone);
        switch ($data['sex']){
            case '0':
                $data['sex'] = '男';
                break;
            case '1':
                $data['sex'] = '女';
                break;
        }
        $arr = [
            'id'        =>  $data['id'],
            'username'  =>  $data['username'],
            'sex'       =>  $data['sex'],
            'email'     =>  $data['email'],
            'phone'     =>  $data['phone'],
            'header'    =>  $data['header'],
            'type'      =>  'Buyer'
        ];
        app('mycache')->set($token,json_encode($arr),86400);
        return $token;
    }
}